package com.cnsuning.jobtool.db22db2;

import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class GenerateCopySql {
    /**
     * 配置路径
     */
    String configPath = "src/test/java/com/cnsuning/jobtool/db22db2/tables.json";

    /**
     * 生成配置
     */
    String configJson;

    /**
     * sql模板文件
     */
    String templeteSql;

    /**
     * 生成文件路径
     */
    String finalFilePath = "C:\\Users\\徐兴兴\\Desktop\\sit环境db2 ddl\\genarate\\genarate";
//    String finalFilePath = "C:\\Users\\徐兴兴\\Desktop\\generate_test.sql";


    String prefixSql;

    @Before
    public void setup()
    {
        try {
            configJson = FileUtils.readFileToString(new File(configPath),"utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(configJson);

//        templeteSql = "declare mycur CURSOR database {dbname} user {username} using {password} for select * from {srcSchema}.{srcTableName} with ur;\n" +
//                "load from mycur of CURSOR insert into {targetSchema}.{targetTableName} nonrecoverable;\n";

//        templeteSql = "declare mycur CURSOR database %s user %s using %s for select * from %s.%s with ur;\n" +
//                "load from mycur of CURSOR insert into %s.%s nonrecoverable;\n";

        templeteSql = "declare mycur CURSOR database %s user %s using '%s' for select * from %s.%s with ur;\n" +
                "load from mycur of CURSOR insert into %s.%s nonrecoverable;\n";


        prefixSql  = "catalog tcpip node srcnode remote %s server %s;\n" +
                "catalog database %s at node srcnode authentication server;\n";
    }

    @Test
    public void generate()
    {
        List<ConfigBean> configList = JSON.parseArray(configJson,ConfigBean.class);
        for (int i = 0; i < configList.size(); i++)
        {
            ConfigBean config = configList.get(i);
            String tableNames = config.getTableNames();
            String srcDbName = config.getSrcDbName();
            String srcSchema = config.getSrcSchema();
            String srcUserName = config.getSrcUserName();
            String srcPwd = config.getSrcPwd();
            String targetSchema = config.getTargetSchema();
            String[] tables = tableNames.split(",");
            String dbFileName = finalFilePath+"_db"+(i+1)+".sql";

            File file = new File(dbFileName);
            if (file.exists())
            {
                file.delete();
            }

            try {
                String prefixTemp = String.format(prefixSql,"192.168.40.24","60000",srcDbName);
                FileUtils.writeStringToFile(file,prefixTemp,"utf-8",true);

                for (String tableName : tables)
                {
                    String tempString = String.format(templeteSql,srcDbName,srcUserName,srcPwd,srcSchema,tableName,targetSchema,tableName);
                    FileUtils.writeStringToFile(file,tempString,"utf-8",true);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
